package cn.eclass.utils;

import java.util.ArrayList;
import java.util.List;

public class Page {
	//max pages.
	private int maxPage = 0;
	//items per page.
	private int itemPerPage = 12;
	//total items.
	private int maxItem = 0;
	private String commonUrl = "";
	private int maxActivePages = 3; //assume odd.
	
//	public Page(int itemPerPage, int maxItem, String commonUrl) {
//		this.itemPerPage = itemPerPage;
//		this.maxItem = maxItem;
//		maxPage = maxItem/itemPerPage;
//		this.commonUrl = commonUrl;
//	}

	public void setCommonUrl(String theCommonUrl) {
		this.commonUrl = theCommonUrl;
		if(!commonUrl.contains("?"))commonUrl+="?";
	}

	public String getCommonUrl(){
		return commonUrl;
	}	

	public int getItemPerPage() {
		return itemPerPage;
	}

	public void setItemPerPage(int itemPerPage) {
		this.itemPerPage = itemPerPage;
		maxPage = maxItem/itemPerPage;
	}
	
	public int getMaxItem() {
		return maxItem;
	}

	public void setMaxItem(int maxItem) {
		maxPage = maxItem/itemPerPage;
		this.maxItem = maxItem;
	}

	//page starts from 1, ends at maxPage.
	//item starts from 1.
	public int getOffSet(int page){
		return itemPerPage*(page-1);
	}
	
	//Show 
	public List<Integer>getShowPages(int page){
		ArrayList<Integer> list = new ArrayList<Integer>();
		list.add(1);
		list.add(2);
		return list;
	}
	
	public String getUrl(int page){
		return getCommonUrl()+"pageId="+page;
	}
	
	public int getMaxPage() {
		return maxPage;
	}
	
	public void setMaxActivePages(int maxActivePages) {
		this.maxActivePages = maxActivePages;
	}
	
	public int getMaxActivePages() {
		return maxActivePages;
	}
	
	public List<Integer> getActiveNumbers(int page) {
		ArrayList list = new ArrayList<Integer>();
		if(maxPage < maxActivePages+1) {
			for(int i = 1; i <= maxPage; i++) list.add(i);
			return list;
		}
		//Assume odd.
		int pageRad = maxActivePages/2;
		for(int i = -pageRad; i <= pageRad; i++){
			list.add(page+i);
		}
		return list;
	}
	
//	public List<String> getPageUrls(int page) {
//		
//	}
	

}
